<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="handleDownloadBill"> 下载对账单</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)"/>
      </template>
    </BasicTable>
    <DownloadModal @register="registerDownloadModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="PayBillDownload" setup>
import {BasicTable, TableAction} from '/@/components/Table';
import {useModal} from '/@/components/Modal';
import DownloadModal from './DownloadModal.vue';
import {useGlobSetting} from '/@/hooks/setting';
import {columns, searchFormSchema} from './bill.download.data';
import {batchDeleteBill, deleteBill, getExportUrl, getList} from './bill.download.api';
import {useListPage} from '/@/hooks/system/useListPage';
import {useMessage} from '/@/hooks/web/useMessage';
import {Icon} from '/@/components/Icon'

const { createMessage } = useMessage();
const glob = useGlobSetting();
const [registerDownloadModal, { openModal: openDownloadModal }] = useModal();
// 列表页面公共参数、方法
const { onExportXls, tableContext } = useListPage({
  designScope: 'PayBillDownload',
  tableProps: {
    title: '账单下载日志列表',
    api: getList,
    columns: columns,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  },
  exportConfig: {
    name: "账单下载日志列表",
    url: getExportUrl,
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

const handleDownloadBill = async () => {
  openDownloadModal(true, {})
}

/**
 * 操作列定义
 * @param record
 */
 function getActions(record) {
  return [
    {
      label: '删除',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    }
  ]
}

/**
 * 删除
 */
const handleDelete = async (record) => {
  await deleteBill({ id: record.id }, () => {
    reload()
    createMessage.success("删除成功")
  })
  
}
/**
 * 批量删除事件
 */
 async function batchHandleDelete() {
  await batchDeleteBill({ ids: selectedRowKeys.value }, () => {
    reload()
    createMessage.success("删除成功")
  });
}
</script>
